package app.temporary;

import app.common.utils.IdGenerator;
import com.gitee.feizns.dynamic.DateUtils;

import java.time.LocalDateTime;

/**
 * 代码生成器
 * @author feizns
 * @since 2023/08/15
 */
public class CodeGenerator {

    /**
     * 前缀
     */
    private final String prefix;

    /**
     * 数字位数
     */
    private final int digits;

    /**
     * 编号发电机
     */
    private final IdGenerator idGenerator = new IdGenerator();

    public CodeGenerator(String prefix, int digits) {
        this.prefix = prefix;
        this.digits = digits;
    }

//    /**
//     * 代码
//     * @param date 日期
//     * @param no 没有
//     * @return {@link String}
//     */
//    public String code(String date, String no) {
//        LocalDateTime time = DateUtils.parse(date, LocalDateTime.class);
//        String yyyyMMddHHmm = DateUtils.format(time, "yyyyMMddHHmm");
//        String key = yyyyMMddHHmm + "-" + no;
//        return String.format("%s%s%0" + digits + "d", prefix, yyyyMMddHHmm, idGenerator.next(key));
//    }

    /**
     * 代码
     * @param date 日期
     * @return {@link String}
     */
    public String code(String date) {
        LocalDateTime time = DateUtils.parse(date, LocalDateTime.class);
        String yyyyMMddHHmm = DateUtils.format(time, "yyyyMMddHHmm");
        return String.format("%s%s%0" + digits + "d", prefix, yyyyMMddHHmm, idGenerator.next(yyyyMMddHHmm));
    }

//    public static void main(String[] args) {
//        CodeGenerator generator = new CodeGenerator("RKY", 3);
//        System.out.println(generator.code("2023-09-10", "123"));
//        System.out.println(generator.code("2023-09-10", "123"));
//        System.out.println(generator.code("2023-09-10", "123"));
//    }

}
